const DB = require('../db/index');
const enumData = require('../enumeration/index');
const exception = require('../exception');
const fw = require('../core/framework');

module.exports = {
    save(data, successFn, failFn){
        this.findByCode(data.code, (dbRes) => {
            try {
                DB.Config.update(data).then((res) => {
                    if (fw.isFn(successFn))successFn({code: 0,data:res})
                });
            } catch (e) {
                if (fw.isFn(failFn))failFn(exception.ex(e))
            }
        }, (e) => {
            if (e) {
                if (fw.isFn(failFn))failFn(exception.ex(e))
            } else {
                DB.Config.create(data).then((res) => {
                    if (fw.isFn(successFn))successFn({code: 0,data:res})
                });
            }
        });
    },
    hasNewDevice(data, successFn, failFn){
        let param = {
            code: 'device'
        };
        if(data.value) param.value = data.value;
        DB.Config.getValue(param).then((dbResData) => {
            let dbRes = fw.getDBObject(dbResData);
            if (fw.isExit(dbRes)) {
                if (fw.isFn(successFn)) successFn(dbRes);
            } else {
                if (fw.isFn(failFn)) failFn();
            }
        })
    },
    hasNewFace(data, successFn, failFn){
        let param = {
            code: 'face'
        };
        if(data.value) param.value = data.value;
        DB.Config.getValue(param).then((dbResData) => {
            let dbRes = fw.getDBObject(dbResData);
            if (fw.isExit(dbRes)) {
                if (fw.isFn(successFn)) successFn(dbRes);
            } else {
                if (fw.isFn(failFn)) failFn();
            }
        })
    },
    create(data, successFn, failFn) {
        try {
            DB.Config.create(data).then((res) => {
                successFn({code: 0,data:res})
            });
        } catch (e) {
            failFn(exception.ex(e))
        }
    },
    update(data, successFn, failFn) {
        this.findById(data.id, (dbRes) => {
            try {
                DB.Config.update(data).then(() => {
                    successFn({code: 0})
                });
            } catch (e) {
                failFn(exception.ex(e))
            }
        }, (e) => {
            if (e) {
                failFn(exception.ex(e))
            } else {
                failFn(exception['1440'])
            }
        });
    },
    search(data, successFn, failFn) {
        try {
            DB.Config.searchAll(data).then((res) => {
                successFn({
                    code: 0, data: res
                });
            });
        } catch (e) {
            failFn(exception.ex(e))
        }
    },
    delete(data, successFn, failFn) {
        this.findById(data.id, (dbRes) => {
            try {
                DB.Config.delete({id: data.id}).then(() => {
                    successFn({code: 0})
                });
            } catch (e) {
                failFn(exception.ex(e))
            }

        }, (e) => {
            e ? failFn(exception.ex(e)) : failFn(exception['1441'])
        });
    },
    findByCode(code, successFn, failFn) {
        try {
            DB.Config.search({code: code}).then((dbResData) => {
                let dbRes = fw.getDBObject(dbResData);
                if (fw.isExit(dbRes)) {
                    if (fw.isFn(successFn)) successFn({code:0,data:dbRes});
                } else {
                    if (fw.isFn(failFn)) failFn();
                }
            })
        } catch (e) {
            if (fw.isFn(failFn)) failFn(e);
        }
    }
};
